/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.eisti.icc.zk;

import java.util.HashMap;
import java.util.Map;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author rogers
 */
public class EcouteurLogin extends GenericForwardComposer {
    
    private Window loginWin;
    private Textbox name;
    private Textbox pwd;
    private Label mesg;
    private Button loginBtn;
    private Div recherche;
    private Div affichage;
    
    public void onClick$loginBtn() {
        if (loginBtn.getLabel().equals("Se connecter")) {
            if (!loginWin.isVisible()) {
                loginWin.setVisible(true);
            }else{
                loginWin.doHighlighted();
            }
        }else{
            throw new UnsupportedOperationException("Deconnexion");
        }
    }
    
    public void onClick$valider() {
        loginCheck();
    }
    
    
    private void loginCheck () {
        if ((name.getValue().equals("rogereric") && pwd.getValue().equals("admin"))) {
            mesg.setValue("");
            loginWin.setVisible(false);
            loginBtn.setLabel("Deconnexion");
            Map dataProfil = new HashMap();
            
            dataProfil.put("name", "Roger Eric");
            dataProfil.put("promo", "2k13");
            dataProfil.put("numero", "N° 16456489748974");
            dataProfil.put("adresse", "30 route de Lahourcade 64150 Noguères");
            dataProfil.put("tel", "0681315984");
            
            Map dataEmprunts = new HashMap();
            
            dataProfil.put("emprunt", "emprunt1");
            
            Executions.createComponents("rechercheEtProfil.zul", recherche, dataProfil);
            Executions.createComponents("affichage.zul", affichage, null);
            Executions.createComponents("listeEmprunts.zul", affichage, dataEmprunts);
        } else {
            mesg.setValue("Nom d'utilisateur ou mot de passe incorrect !!!");
            Clients.evalJavaScript("loginFaild()");
        }
    }

}
